package com.itheima.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import com.itheima.utils.QiniuUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.Set;
import java.util.UUID;

@RestController
@RequestMapping("/setmeal/")
public class SetmealController {

    @Reference(timeout = 7000)
    private SetmealService setmealService;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    private static final String SETMEAL = "SETMEAL_FIND_PAGE";

    @RequestMapping("findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {

        PageResult pageResult = null;
        try {
            String hashKey = queryPageBean.getCurrentPage()+":"+queryPageBean.getPageSize();
            HashOperations<String, Object, Object> hash = redisTemplate.opsForHash();
            Object data = hash.get(SETMEAL,hashKey);
            if(data == null){
                pageResult = setmealService.pageQuery(queryPageBean);
                hash.put(SETMEAL,hashKey,pageResult);
            }else{
                pageResult = ((JSONObject)data).toJavaObject(PageResult.class);
            }
        } catch (Exception e) {
            pageResult = setmealService.pageQuery(queryPageBean);
        }

        return pageResult;
    }

    @RequestMapping("upload")
    public Result upload(MultipartFile imgFile, String setmeal, Integer[] i) {
        try {

            //获取原始文件名
            String originalFilename = imgFile.getOriginalFilename();
            int lastIndexOf = originalFilename.lastIndexOf(".");
            //获取文件后缀
            String suffix = originalFilename.substring(lastIndexOf);
            //使用UUID随机产生文件名称，防止同名文件覆盖
            String fileName = UUID.randomUUID().toString() + suffix;

            QiniuUtils.upload2Qiniu(imgFile.getBytes(), fileName);
            Setmeal setmealTemp = JSONObject.parseObject(setmeal, Setmeal.class);
            setmealTemp.setImg(fileName);

            setmealService.add(setmealTemp, i);
            deleteRedisData();
            return new Result(true, MessageConstant.ADD_SETMEAL_SUCCESS);
        } catch (RuntimeException e) {
            return new Result(false, e.getMessage());
        } catch (Exception ioException) {
            return new Result(false, MessageConstant.PIC_UPLOAD_SUCCESS);
        }
    }

    public void deleteRedisData(){
        HashOperations<String, Object, Object> hash = redisTemplate.opsForHash();
        Set<Object> keys = hash.keys(SETMEAL);
        System.out.println(keys);
        keys.forEach(o -> {
            hash.delete(SETMEAL,o);
        });
    }
}
